<?php

    namespace yaliSDK\live;

    use think\Exception;
    use yaliSDK\common\BaseRepository;
    use yaliSDK\common\IRepository;

    class AnchorFollowRepository extends BaseRepository implements IRepository {
        private static $TABLE = 'live_follow';
        function save($data, $option = []): bool {
            if ($this->db->name(self::$TABLE)->insert($data) > 0) {
                return true;
            }
            return false;
        }

        function get($id, $option = []) {
            $result = null;
            try {
                $key = $option['key'] ?? 'id';
                $result = $this->db->name(self::$TABLE)->where([$key => $id])->find();
            } catch (Exception $e) {

            }
            return $result;
        }

        function lists($option = []) {
            $result = null;

            try {

                $cb = function () use ($option) {
                    $uid = $option['uid'] ?? null;
                    $web_id = $option['web_id'] ?? null;
                    $query = $this->db
                        ->name(self::$TABLE)
                        ->alias('l');


                    if ($uid) {
                        $query->where(['l.uid' => $uid, 'l.web_id' => $web_id]);
                    }
                    $query->order(['l.addtime' => 'desc'])->field('l.*');
                    return $query;
                };

                $total = $cb()->count();// 当前总数
                $isCount = $option['isCount'] ?? null;// 仅统计数量
                if (empty($isCount)) {
                    $page = $option['page'] ?? 1;
                    $limit = $option['rows'] ?? 20;
                    $result = $cb()->page($page, $limit)->select();
                }
                return [
                    'list' => $result,
                    'total' => $total,
                ];
            } catch (Exception $e) {
            }
        }

        function update($data, $option = []): bool {

        }
        function isExist($id, $option): bool {
            $key = $option['key'] ?? 'id';
            return $this->db->name(self::$TABLE)->where([$key=>$id])->count('id') > 0;
        }
    }